package com.ruoyi.generator.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.generator.domain.FromTableColumnConfig;
import com.ruoyi.generator.service.IFromTableColumnConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据列自定义配置__是否展示列、排序等Controller
 *
 * @author ZGL
 * @date 2025-04-07
 */
@RestController
@RequestMapping("/generator/columnConfig")
public class FromTableColumnConfigController extends BaseController
{
    @Autowired
    private IFromTableColumnConfigService fromTableColumnConfigService;

    /**
     * 修改数据列自定义配置__是否展示列、排序等
     */
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Map columnConfigs)
    {
        if(columnConfigs ==null){
            return AjaxResult.error("操作失败，请求参数为空");
        }
        //表单配置标识
        String formBuildKey = String.valueOf(columnConfigs.get("formBuildKey"));
        Object fieldInfo = columnConfigs.get("fieldInfo");
        if(StringUtils.isEmptyIncludeNull(formBuildKey)){
            return AjaxResult.error("操作失败，表单配置标识参数为空");
        }
        if(fieldInfo == null){
            return AjaxResult.error("操作失败，字段信息为空");
        }
        List<FromTableColumnConfig> fromTableColumnConfigs = JSONArray.parseArray(JSONObject.toJSONString(fieldInfo), FromTableColumnConfig.class);
        return toAjax(fromTableColumnConfigService.save(formBuildKey,fromTableColumnConfigs));
    }

    @GetMapping("/reset/{formBuildKey}")
    public AjaxResult save(@PathVariable("formBuildKey") String formBuildKey)
    {
        try{
            if(StringUtils.isEmptyIncludeNull(formBuildKey)){
                return AjaxResult.error("操作失败，表单配置标识参数为空");
            }
            //获取平台用户信息
            LoginUser loginUser = SecurityUtils.getLoginUser();
            /**
             * 删除之前的配置信息
             */
            fromTableColumnConfigService.deleteTableColumnConfigByBuildKey(formBuildKey,loginUser.getUsername());

            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error("操作失败，原因："+e.getMessage());
        }
    }
}
